﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace shelltwit_tester
{
	/// <summary>
	/// Summary description for UnitTest1
	/// </summary>
	[TestClass]
	public class Status
	{
		public Status()
		{
			shelltwitlib.TwitterUser.ClearCredentials();

			XmlDocument doc = new XmlDocument();
			doc.LoadXml("<TwitterUser OAuthToken=\"108356361-MJUGgWxlbcByH23kVKZGUutU3pWnWunonWqDEZBp\" OAuthTokenSecret=\"scb9AE7iVGp1OCvqPsjqU2iTUtn50eGClbXpwt97cRM\" />");
			doc.Save(@"C:\Users\sgomez\AppData\Local\twit.usr");

		}

		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassInitialize to run code before running the first test in the class
		// [ClassInitialize()]
		// public static void MyClassInitialize(TestContext testContext) { }
		//
		// Use ClassCleanup to run code after all tests in a class have run
		// [ClassCleanup()]
		// public static void MyClassCleanup() { }
		//
		// Use TestInitialize to run code before running each test 
		// [TestInitialize()]
		// public void MyTestInitialize() { }
		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion

		[TestMethod]
		public void Basic()
		{
			string status = string.Format("Hello World:{0}",DateTime.Now);
			try
			{
				string response = shelltwitlib.Status.Update.UpdateStatus(status);
				Assert.AreEqual(response, "OK");
			}
			catch (Exception ex)
			{
				ManageException(ex);
			}
		}

		//[TestMethod]
		//public void Largo()
		//{
		//    string status = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
		//    try
		//    {
		//        string response = shelltwitlib.Status.Update.UpdateStatus(status);
		//        Assert.AreEqual(response, "OK");
		//    }
		//    catch (Exception ex)
		//    {
		//        ManageException(ex);
		//    }
		//}

		[TestMethod]
		public void CaracteresEspeciales()
		{
			string status = string.Format("Los tildes son á é í ó ú y la ñ. Las myúsculas son Á É Í Ó Ú Ñ: {0}",DateTime.Now);
			try
			{
				string response = shelltwitlib.Status.Update.UpdateStatus(status);
				Assert.AreEqual(response, "OK");
			}
			catch (Exception ex)
			{
				ManageException(ex);
			}
		}

		[TestMethod]
		public void AConTilde()
		{
			string status = string.Format("á:{0}",DateTime.Now);
			try
			{
				string response = shelltwitlib.Status.Update.UpdateStatus(status);
				Assert.AreEqual(response, "OK");
			}
			catch (Exception ex)
			{
				ManageException(ex);
			}
		}

		[TestMethod]
		public void Exclamacion()
		{
			string status = string.Format("!:{0}",DateTime.Now);
			try
			{
				string response = shelltwitlib.Status.Update.UpdateStatus(status);
				Assert.AreEqual(response, "OK");
			}
			catch (Exception ex)
			{
				ManageException(ex);
			}
		}

		[TestMethod]
		public void ElProblematico()
		{
			string status = string.Format("des-i5 y la reconchaqueteparió!!!:{0}", DateTime.Now);
			try
			{
				string response = shelltwitlib.Status.Update.UpdateStatus(status);
				Assert.AreEqual(response, "OK");
			}
			catch (Exception ex)
			{
				ManageException(ex);
			}
		}

		//

		void ManageException(Exception ex)
		{
			string message = ex.Message;
			WebException wex = ex as WebException;
			if (wex != null) 
			{
				HttpWebResponse res = (HttpWebResponse)wex.Response;
				if (res != null)
				{
					using (Stream str = res.GetResponseStream())
					using (StreamReader reader = new StreamReader(str))
						message = reader.ReadToEnd();
				}
			}

			Assert.Fail(message);
		}
	}
}
